﻿/*
* Author:  YPN
* Created: 2022/07/02 17:32:24
* Contact: yangpiena@163.com
* Description: 
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ypn.common.csharp
{
    public class DataGridHelper
    {
        /// <summary>
        /// 初始化表格
        /// </summary>
        /// <param name="i_DataGridView"></param>
        /// <param name="i_RowHeight"></param>
        public static void InitDataGrid(DataGridView i_DataGridView, int i_RowHeight = 23)
        {
            i_DataGridView.AllowUserToAddRows = false;
            i_DataGridView.AutoGenerateColumns = false;
            i_DataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            i_DataGridView.ReadOnly = true;
            // 行
            i_DataGridView.RowTemplate.Height = i_RowHeight;
            i_DataGridView.RowHeadersWidth = 60;
            i_DataGridView.RowStateChanged += I_DataGridView_RowStateChanged;
            i_DataGridView.TopLeftHeaderCell.Value = "序号";
            i_DataGridView.TopLeftHeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
        }

        private static void I_DataGridView_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
        {
            e.Row.HeaderCell.Value = string.Format("{0}", e.Row.Index + 1);
        }

        /// <summary>
        /// 初始化带分页栏的表格
        /// </summary>
        /// <param name="i_DataGridView"></param>
        /// <param name="i_RowHeight"></param>
        public static void InitDataGridWithPagination(DataGridView i_DataGridView, int i_RowHeight = 23)
        {
            i_DataGridView.AllowUserToAddRows = false;
            i_DataGridView.AutoGenerateColumns = false;
            i_DataGridView.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            i_DataGridView.ReadOnly = true;
            // 行
            i_DataGridView.RowTemplate.Height = i_RowHeight;
            i_DataGridView.RowHeadersWidth = 60;
            i_DataGridView.TopLeftHeaderCell.Value = "序号";
            i_DataGridView.TopLeftHeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;
        }

        /// <summary>
        /// 初始化表格（可编辑）
        /// </summary>
        /// <param name="i_DataGridView"></param>
        /// <param name="i_RowHeight"></param>
        public static void InitDataGridAndEdit(DataGridView i_DataGridView, int i_RowHeight = 23)
        {
            i_DataGridView.AllowUserToAddRows = true;
            i_DataGridView.AutoGenerateColumns = false;
            i_DataGridView.SelectionMode = DataGridViewSelectionMode.CellSelect;
            i_DataGridView.ReadOnly = false;
            // 行
            i_DataGridView.RowTemplate.Height = i_RowHeight;
        }
    }
}
